Recursion is a building block every programmer should have in their abilities to be able to solve problems. We can write a solution to a problem the non-recursive way which is called the “Iterative ” or the “recursive” approach. Many people find difficulty in grasping the concept of recursion. Follow me in this article as I break the concept down to help you understand.
What is Recursion?
Looking at recursion in a sense of a 4 x 100 meters relay racing.
A 4 x100 meters relay consist of four teams with four members in each team. Each member covers 100-meter distance, totalling to 400 meters covered per team.
The idea here is taking a problem and doing it over and over on a smaller piece or a changing piece until you hit a certain endpoint.
Let’s consider the operation of covering 100-meter distance and giving the baton stick to the next team member. Assuming we have a function that implements this logic, each member continues the distance until he covers 100 meters to pass the baton stick to the next member. (i.e: By calling the function to run 100 meters with the second team member).
Later in the article, we will look at some code implementation of some recursive problems. But for now, let look at the essential part of a recursive function
Essential Parts Of a Recursive function
For every recursive function, two things need to be present. As said, the goal is to call the same function with different input until you reach a base case ( A stopping condition ). Now let look at the two essential part every recursive function should have.
- Base-Case:
The most important concept to understand is that without a base case, your recursive function will run till infinity, resulting in a ‘Stack Overflow’. The base-case of every recursive function is the stopping piece, the condition that stops the recursive calls. This concept, just like ‘while loop’ has to have a false condition to break out of the loop.
- The Recursive Call:
This is the act of calling the function over and over again. The recursive call should always be with different input. Calling the function with the same input will lead to a never-ending loop ( Stack-Overflow)
some code implementations of a recursive function:
Code Sample:
‘Write a function that takes in a string as an argument and print out all the characters in that string’.
To begin with, let solve this problem using an iterative approach, which is pretty simple.
const PrintOutIteratively = (str) => {
for (let i = 0; i < str.length; i++) {
console.log(str[i]);
}
};Here, we loop through the characters of a string, for each iteration we print out the character at the loops index.
let’s look at how to solve it using recursion
const PrintOutRecursively = (name) => {
i = 0;
if (i >= name.length) {
console.log(name[i]);
return;
}
console.log(name[i]);
i++;
return PrintOutRecursively(name.substr(i));
};“Looking at this solution, try and figure out “the base case” and “the recursive call” If you did this right, Congratulations you have unlocked recursion.
The Base-Case:
if (i >= name.length) {
console.log(name[i]);
return;
}The terminating clause: here, once the value of i equals the length of the string we return or we continue with the process.
The Recursive Call:
return PrintOutRecursively(Name.substr(i) )
Notice: for each recursive call, the value of the string won't be the same since we pass in the same string except the value at index i.
Conclusion
I hope this has helped you gain a solid understanding of recursion.